package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Comment;
import com.itheima.pojo.User;
import com.itheima.service.CommentService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.CommentVO;
import com.itheima.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     *@param commentVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/add")
    public String add(@RequestBody CommentVO commentVO) {
        try {
            commentService.add(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED, MessageConstant.COMMENT_ADD_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.COMMENT_ADD_SUCCESS);
    }


    /**
     * 关闭评论邮件通知
     *@param userUid
     * @return java.lang.String
     *@author itheima
     */
    @GetMapping("/closeEmailNotification/{userUid}")
    public String closeEmailNotification(@PathVariable("userUid") String userUid) {
        Boolean flag = commentService.closeEmailNotification(userUid);
        if (flag) {
            return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.COMMENT_CLOSE_EMAIL_NOTIFICATION_SUCCESS);
        } else {
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_CLOSE_EMAIL_NOTIFICATION_FAIL);
        }
    }

    /**
     * 删除评论
     *@param commentVO
     * @return java.lang.String
     *@author itheima
     */
    @PostMapping("/delete")
    public String delete(@RequestBody CommentVO commentVO) {
        try {
            commentService.delete(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_DELETE_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.COMMENT_DELETE_SUCCESS);
    }

    /**
     * 获取评论列表
     *@param commentVO
     * @return java.lang.String
     *@author itheima
     */
    @PostMapping("/getList")
    public String getList(@RequestBody CommentVO commentVO) {
        List<Comment> comments = commentService.getList(commentVO);
        return ResultUtil.result(HttpServletResponse.SC_OK,comments);
    }

    /**
     * 获取评论列表和回复
     *@param userVO
     * @return java.lang.String
     *@author itheima
     */
    @PostMapping("/getListByUser")
    public String getListByUser(@RequestBody UserVO userVO) {
        List<User> userList = commentService.getListByUser(userVO);
        return ResultUtil.result(HttpServletResponse.SC_OK,userList);
    }

    /**
     * 获取用户点赞信息
     *@param currentPage
    	 * @param pageSize
     * @return java.lang.String
     *@author itheima
     */
    @PostMapping("/getPraiseListByUser")
    public String getPraiseListByUser(@RequestParam("currentPage") long currentPage, @RequestParam("pageSize") long pageSize) {
        commentService.getPraiseListByUser();
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.COMMENT_FINDPRAISE_SUCCESS);
    }

    /**
     * 举报评论
     *@param commentVO
     * @return java.lang.String
     *@author itheima
     */
    @PostMapping("/report")
    public String report(@RequestBody CommentVO commentVO) {
        int count = commentService.report(commentVO);
        if (count > 0) {
            return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.COMMENT_REPOTR_SUCCESS);
        } else {
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_REPOTR_FAIL);
        }
    }

}
